package com.restfinder.common.component.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import java.util.Properties;

import com.restfinder.common.component.Message;
import com.restfinder.user.service.UserService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MessageUtils {
	
	private static final String FILE_MESSAGES_PROPERTIES	= "messages_pl.properties";
	private static final String MESSAGE_PROPERTIES_PREFIX	= "com.restfinder.";
	

	public static String getMessage(String key){
		return loadJavaUtilProperties(FILE_MESSAGES_PROPERTIES).getProperty(MESSAGE_PROPERTIES_PREFIX+key, "");
	}
	
	public static Message createMessage(String titleKey, String contentKey){
		return new Message(getMessage(titleKey), getMessage(contentKey));
	}
	
	public static Message createMessage(String titleKey, Exception e){
		return new Message(getMessage(titleKey), e.getMessage());
	}

    private static Properties loadJavaUtilProperties(String name) {
		try {
			ClassLoader loader = UserService.class.getClassLoader();
			Properties properties = new Properties();
			properties.load(loader.getResourceAsStream(name));
			return properties;
		} catch (Exception e) {
			log.error("Exception while loading properties file",e);
		}
		return null;
	}
}
